﻿using AbpApplication.Expand;
using AbpEntityFrameworkcore.DbContext;
using AbpToDomain.EntityInfo;
using Mesosphere.Dto;
using Mesosphere.InPut;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace AbpApplication.AppServices
{
    public class ConsumerService : ApplicationService, IConsumerService
    {

        private readonly IRepository<TaskOrder> taskorder_res;
        private readonly IRepository<NeetOutMaterial> neetout_res;
        private readonly IRepository<MaterialBase> material_res;
        private readonly IRepository<NeetOutMaterial> neetoutmaterial_res;
        private readonly IRepository<PutInOrder> put_in_res;
        private readonly IRepository<PutOutOrder> put_out_res;
        private readonly MyDbContext dbContext;
        private readonly IRepository<MaterialProperty> MaterialProperty_res;
        private readonly ILoginHelper loginHelper;

        public ConsumerService(IRepository<TaskOrder> taskorder_res,
            IRepository<NeetOutMaterial> neetout_res,
            IRepository<MaterialBase> material_res,
            IRepository<NeetOutMaterial> neetoutmaterial_res, ILoginHelper loginHelper,
IRepository<MaterialProperty> materialProperty_res, IRepository<PutInOrder> put_in_res, MyDbContext dbContext, IRepository<PutOutOrder> put_out_res)

        {
            this.taskorder_res = taskorder_res;
            this.neetout_res = neetout_res;
            this.material_res = material_res;
            this.neetoutmaterial_res = neetoutmaterial_res;
            this.loginHelper = loginHelper;
            MaterialProperty_res = materialProperty_res;
            this.put_in_res = put_in_res;
            this.dbContext = dbContext;
            this.put_out_res = put_out_res;
        }

        public async Task<ApiResponse> CreateInBoundOrder(InPut_CunsumerService InPut)
        {
            try
            {
                var PutInorderDataList = await put_in_res.WithDetails().ToListAsync();
                bool Check_res = PutInorderDataList.Any(x => InPut.putInOrderDtos. Any(y => y.Consumer_MaterialId == x.Consumer_MaterialId));
                if (Check_res)
                {
                    return new ApiResponse("WMS已存在相同Id的入库清单", false, null);
                }
                bool Check_res2 = PutInorderDataList.Any(x => x.Symbol==InPut.Symbol);
                if (Check_res2)
                {
                    return new ApiResponse("WMS已存在相同清单号", false, null);
                }
                List<PutInOrder> putInOrderModel = ObjectMapper.Map<List<PutInOrderDto>, List<PutInOrder>>(InPut.putInOrderDtos);
                putInOrderModel.ForEach(x =>
                {
                    x.Symbol = InPut.Symbol;
                    x.State = "待入库";
                    x.CreateTime = DateTime.Now;
                });
                await put_in_res.InsertManyAsync(putInOrderModel);
                return new ApiResponse("添加成功", true, null);
            }
            catch (Exception ex)
            {


                loginHelper.LoginAsync(InPut, "Error-CreateInBoundOrder");
                return new ApiResponse("Error-CreateInBoundOrder", false, null);
            }
        }

        public async Task<ApiResponse> CreateOutBoundOrder(InPut_CunsumerService2 InPut)
        {
            try
            {
                var PutOutorderDataList = await put_out_res.WithDetails().ToListAsync();
                bool Check_res = PutOutorderDataList.Any(x => x.Symbol == InPut.Symbol);
                if (Check_res)
                {
                    return new ApiResponse("WMS已存在相同出库清单号", false, null);
                }
                List<PutOutOrder> putOutOrderModel = ObjectMapper.Map<List<PutOutOrderDto>, List<PutOutOrder>>(InPut.outOrderDtos);
                putOutOrderModel.ForEach(x =>
                {
                    x.Symbol = InPut.Symbol;
                    x.State = "待分配";
                    x.CreateTime = DateTime.Now;
                });
                await put_out_res.InsertManyAsync(putOutOrderModel);
                return new ApiResponse("添加成功", true, null);
            }
            catch (Exception ex)
            {


                loginHelper.LoginAsync(InPut, "Error-CreateOutBoundOrder");
                return new ApiResponse("Error-CreateOutBoundOrder", false, null);
            }
        }

        public async Task<ApiResponse> GetMaterialInfo(InPut_CunsumerService3 InPut)
        {
            throw new NotImplementedException();
        }

        public async Task<ApiResponse> MakeInventory(InPut_CunsumerService4 InPut)
        {
            throw new NotImplementedException();
        }
    }
}
